const app = getApp()
Page({
  data: {
    currentTab: 0,
    params: {
      auditStatus: '',
      currentPage: 1,
      pageSize: 10,
    },
    courseLibraryList: [],
    total: 0,
    roleType:'',
    hiddenModal: true,
    coachingSessionId: '', // 审核不通过的id
    permissionCodes:[]
  },
  onLoad: function (options) {
    const userInfo = JSON.parse(wx.getStorageSync('userInfo'))
    console.log(userInfo,'userInfo===============');
    const permissionCodes = app.globalData.permissionCodes || []
    let roletypeIfmange = permissionCodes.includes('exam_manage')? '1' : '2'
    this.setData({
      roleType:roletypeIfmange,
      permissionCodes:permissionCodes
    })
    // console.log(userInfo, roletypeIfmange)
  },
  onShow: function (params) {
    this.setData({
      'params.currentPage': 1
    })
    this.getCourseLibraryList()
  },
  //滑动切换
  swiperTab: function (e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
  },
  async getCourseLibraryList() {
    const { params, roleType ,permissionCodes } = this.data
    const { currentPage } = params
    let url = permissionCodes.includes('exam_manage')?'coaching_vx_admin_session_listBySuperAdmin':'coaching_vx_admin_session_listByPropagandist'
    await app.api.post({
      url: url,
      data: params
    }).then(res => {
      console.log(res)
      if (res.list) {
        let arr = res.list
        for (let i = 0; i < arr.length; i++) {
          arr[i].time = `${arr[i].validityStartTime} - ${arr[i].validityEndTime}`
          if (arr[i].activityStatusLabel) {
            arr[i].status = arr[i].activityStatusLabel 
          }
        }
      }
      let { courseLibraryList } = this.data
      currentPage === 1 ? (courseLibraryList = res.list) : courseLibraryList.push(...res.list)
      this.setData({ courseLibraryList, total: res.total })
    })
  },
  //上下架
  manageItem(e){
    let coachingSessionId = e.currentTarget.dataset.coachingsessionid
    let shelfStatus = e.currentTarget.dataset.shelfstatus
    console.log(shelfStatus,coachingSessionId)
    let data = {coachingSessionId:coachingSessionId}
    let that = this
    let url = shelfStatus==='up'?'coaching_vx_admin_session_shelf_down':'coaching_vx_admin_session_shelf_up'
    wx.showModal({
      title: `${shelfStatus==='up' ? '下' : '上'}架提醒`,
      content: `${shelfStatus==='up' ? '下架后学员不可见此培训活动，确认下架吗？' : '上架后学员可见此培训活动，确认上架吗？'}`,
      cancelColor: '#666666',
      confirmColor: '#006DE7',
      confirmText: `${shelfStatus==='up' ? '下' : '上'}架`,
      success (res) {
        if (res.confirm) {
          app.api.post({
            url: url,
            data
          }).then(res => {
            console.log(res)
            wx.showToast({
              title: '操作成功',
              icon: 'success',
              duration: 1000
            })
            that.getCourseLibraryList()
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //删除项目
  deleteItem(e){
    let coachingSessionId = e.currentTarget.dataset.coachingsessionid
    app.globalData.coachingSessionId = coachingSessionId
    let data ={coachingSessionId:coachingSessionId}
    wx.showModal({
      title: `删除提醒`,
      content: `删除此考试后数据不可恢复，确认删除吗？`,
      cancelColor: '#666666',
      confirmColor: '#006DE7',
      success: (res) => {
        if (res.confirm) {
          app.api.post({
            url: 'coaching_vx_admin_session_delete',
            data
          }).then(res => {
            console.log(res)
            wx.showToast({
              title: '操作成功',
              icon: 'success',
              duration: 1000
            })
            this.getCourseLibraryList()
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //点击切换
  clickTab: function (e) {
    let that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      let code = [
        'init',
        'pass',
        'reject'
      ]
      let auditStatus =code[e.target.dataset.current-1]
      console.log(auditStatus)
      that.setData({
        currentTab: e.target.dataset.current,
        params: {
          auditStatus,
          currentPage: 1,
          pageSize: 10,
        }
      })
      this.getCourseLibraryList(auditStatus)
    }
  },
  newitem() {
    app.globalData.operateType = 'new'
    app.globalData.trainInfo = {}
    wx.navigateTo({
      url: '/myself/pages/train-manage/courseschose/courseschose'
    })
  },
  //编辑
  editItem(e) {
    let coachingSessionId = e.currentTarget.dataset.coachingsessionid
    app.globalData.coachingSessionId = coachingSessionId
    app.globalData.operateType = 'edit'
    app.globalData.trainInfo = {}
    wx.navigateTo({
      url: '/myself/pages/train-manage/courseschose/courseschose'
    })
  },
  detailItem(e){
    let coachingSessionId = e.currentTarget.dataset.coachingsessionid
    app.globalData.coachingSessionId = coachingSessionId
    wx.navigateTo({
      url: '/myself/pages/train-manage/train-detail/tarinDetail'
    })
  },
  loadMore () {
    const { total, courseLibraryList, params } = this.data
    const { currentPage } = params
    if (total > courseLibraryList.length) {
      this.setData({
        'params.currentPage': currentPage + 1
      })
      this.getCourseLibraryList()
    }
  },
  //审核通过
  checkPass(e) {
    let that = this
    const { coachingsessionid: coachingSessionId } = e.currentTarget.dataset
    wx.showModal({
      title: `审核通过`,
      content: `审核通过后培训活动将默认为上架状态，确认审核通过吗？`,
      cancelColor: '#666666',
      confirmColor: '#006DE7',
      success (res) {
        if (res.confirm) {
          app.api.post({
            url: 'coaching_vx_admin_session_audit_pass',
            data: {
              coachingSessionId
            }
          }).then((res) => {
            that.getCourseLibraryList()
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //审核不通过
  rejectModal(e) {
    const { coachingsessionid: coachingSessionId } = e.currentTarget.dataset
    this.setData({
      hiddenModal: false,
      coachingSessionId
    })
  },
  //取消
  rejectCancel() {
    this.setData({
      hiddenModal: true,
      rejectReason: ''
    })
  },
  //审核不通过
  checkReject(e) {
    if (!this.data.rejectReason) {
      wx.showToast({
        title: '不通过理由必填',
        icon: 'none',
        duration: 1000,
      })
      return
    }
    app.api.post({
      url: 'coaching_vx_admin_session_audit_reject',
      data: {
        rejectReason: this.data.rejectReason,
        coachingSessionId:this.data.coachingSessionId
      }
    }).then((res) => {
      wx.showToast({
        title: '操作成功',
        icon: 'success',
        duration: 1000
      })
      this.setData({
        rejectReason:'',
        hiddenModal:true
      })
      this.getCourseLibraryList()
    })
  },
})